package zzuli.edu.cn.aliyunsms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;
import zzuli.edu.cn.aliyunsms.service.SendSMS;

import java.util.HashMap;
import java.util.Map;

@Service
public class SendSMSImpl implements SendSMS {
    @Override
    public boolean sendSMS(String PhoneNumber, String SMSTemplate, Map<String, Object> code) {
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GHDReq5rpZow9Rk6rmH", "7DDIPWxVJZTRC3kMqzf92GLnf69K1y");
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //自定义参数
        request.putQueryParameter("PhoneNumbers", PhoneNumber);
        request.putQueryParameter("SignName","ABC商城");
        request.putQueryParameter("TemplateCode",SMSTemplate);

        //短信验证码
        String smsCode = JSONObject.toJSONString(code);
        request.putQueryParameter("TemplateParam",smsCode);

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
